/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ace.component;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import ace.system.Utilities;

/**
 *
 * @author LegACy
 */
public class ButtonWheel {
	private ButtonWheel(int limit, int selected) {
		//Initialize
		m_X			= 0;
		m_Y			= 0;
		m_Index		= 0;
		m_Buttons	= new Vector();
		m_Draw		= true;

		m_Selection1OffsetX = 0;
		m_Selection1OffsetY = 0;
		m_Selection1Align	= Graphics.LEFT |Graphics.TOP;
		
		//Save
		m_Limit				= limit;
		m_Selected			= selected;
		m_Immediate			= true;		//Defaulted to 'dempet-dempet'

		setImage(null, null);
	}

	public ButtonWheel(int limit, int selected, String selection1, String selection2){
		this(limit, selected);
		setImage(selection1, selection2);
	}

	private void setImage(String selection1, String selection2){
		try {
			if(selection1 == null){
				m_Selection1Avail = false;
			} else {
				m_Selection1Avail = true;
				m_Selection1 = Image.createImage(Utilities.INTERFACE_FOLDER + selection1);
			}

			if(selection2 == null){
				m_Selection2Avail = false;
			} else {
				m_Selection2Avail = true;
				m_Selection2 = Image.createImage(Utilities.INTERFACE_FOLDER + selection2);
			}
		} catch (IOException ex) {}
	}

	public void setImmediate(boolean immediate){ //true = Dempet2 else not
		m_Immediate = immediate;
	}

	public void setSelection1Offset(int x, int y){
		m_Selection1OffsetX = x;
		m_Selection1OffsetY = y;
	}

	public void setSelection1Align(int align){
		m_Selection1Align = align;
	}

	public Button getSelectedButton() {
		return (Button)m_Buttons.elementAt(m_Index);
	}

	public int size(){
		return m_Buttons.size();
	}
	
	public void addButton(int id, String text) {
		//Create and add button
		m_Buttons.addElement(new ButtonPush(text, id, 0, 0));
		((ButtonPush)m_Buttons.lastElement()).setAlignment(Graphics.HCENTER | Graphics.TOP);
		setBtnProperties();
	}

	public void addButton(int id, ButtonPush btn) {
		m_Buttons.addElement(btn);
		setBtnProperties();
	}

	private void setBtnProperties(){
		((ButtonPush)m_Buttons.lastElement()).setPush(false);
	}
	
	public void setPosition(int x, int y) {
		//Set position
		m_X = x;
		m_Y = y;
		
		//Draws
		m_Draw = true;
	}
	
	public void setSelectedID(int id) {
		//Find button id
		for (int i = 0; i < m_Buttons.size(); i++)
			if (((Button)m_Buttons.elementAt(i)).getID() == id) m_Index = i;
	}
	
	public void forceDraw() {
		//Draws when told
		m_Draw = true;
	}
	
	public void update(int keys) {
		if(m_Buttons.size() <= 0) return;

		//Change selection based on key
		if ((keys & GameCanvas.UP_PRESSED) != 0)	m_Index--;
		if ((keys & GameCanvas.DOWN_PRESSED) != 0)	m_Index++;

		//Correct selection
		if (m_Index < 0)					m_Index = m_Buttons.size() - 1;
		if (m_Index >= m_Buttons.size())	m_Index = 0;

		//Restart state
		for (int i = 0; i < m_Buttons.size(); i++) {
			((Button)m_Buttons.elementAt(i)).setState(Button.STATE_NONE);
		}

		//Manage the selected button state
		((Button)m_Buttons.elementAt(m_Index)).setState(Button.STATE_SELECTED);
	}
	
	public void draw(Graphics g) {
		//Skip if shouldn't be drawn
		if (!m_Draw || m_Buttons.size() <= 0) return;
		
		//Get start and end
		int Start	= m_Index - m_Selected;
		int End		= Start + m_Limit;
		if (m_Limit > m_Buttons.size()) End = Start - 1;
		
		//Draws selection
		if(m_Immediate){
			if(m_Selection1Avail)	g.drawImage(m_Selection1, m_X, m_Y + (24 * m_Selected) - 4, m_Selection1Align);
			if(m_Selection2Avail)	g.drawImage(m_Selection2, m_X, m_Y, Graphics.LEFT | Graphics.VCENTER);
		} else {
			if(m_Selection1Avail)	g.drawImage(m_Selection1, getSelectedButton().getX() + m_Selection1OffsetX, getSelectedButton().getY() + m_Selection1OffsetY, m_Selection1Align);
		}
		
		//For each button
		for (int i = Start; i < End; i++) {
			//Get button index
			int Index = i;
			if (Index < 0)					Index = Index + m_Buttons.size();
			if (Index >= m_Buttons.size())	Index = Index % m_Buttons.size();
			
			//Draws button
			if(m_Immediate){
				((Button)m_Buttons.elementAt(Index)).setX(m_X);
				((Button)m_Buttons.elementAt(Index)).setY(m_Y + (24 * (i - Start)));
			}
			
			((Button)m_Buttons.elementAt(Index)).draw(g);
		}
		
		//Not drawn anymore
		m_Draw = false;
	}
	
	//Data
	protected int		m_X;
	protected int		m_Y;
	protected int		m_Limit;
	protected int		m_Index;
	protected int		m_Selected;
	protected Vector	m_Buttons;
	protected boolean 	m_Draw, m_Immediate;

	protected Image		m_Selection1;
	protected Image		m_Selection2;
	protected int		m_Selection1OffsetX, m_Selection1OffsetY;	//Offset Posisi pointer (digunakan hanya jika m_Immediate == false)
	protected int		m_Selection1Align;

	protected boolean	m_Selection1Avail, m_Selection2Avail;
}
